#!/bin/bash
#how2use: put the file to alps dir，first use [./m set], other use view [./m help]
#author:  hutianxi
#version: 20130927




######################init param##############################################
custProjCfg="joytech/custProjCfg";
wgmodemname="joytech72_wet_jb3_hspa";
tgmodemname="joytech72_wet_jb3_tdd128hspa";
op01="OP01_SPEC0200_SEGC";
op02="OP02_SPEC0200_SEGA";
opnone="NONE";
mtkproj="";
custmodem="";
custop="";


######################define func#############################################
function fun_update_params_by_custproj () #update params by current custproj
{
    #get jb3/lca
    mtkproj=`cat $custProjCfg | grep "^\s*${custproj}" | sed 's/.*\s*=\s*//g'`;
    mtkproj=`echo $mtkproj`;
    #echo "mtkproj=$mtkproj";
    if [ "$mtkproj" != "joytech72_wet_jb3" -a "$mtkproj" != "joytech72_wet_lca" ]; then
        echo "mtkproj=$mtkproj not found, err!!!"
        exit;
    fi

    #get modem
    custmodem=`cat joytech/$custproj/mediatek-config-$mtkproj-ProjectConfig.mk | grep "^\s*CUSTOM_MODEM" | sed 's/.*\s*=\s*//g'`;
    custmodem=`echo $custmodem`;
    #echo "custmodem=$custmodem";
    if [ "$custmodem" != "$wgmodemname" -a "$custmodem" != "$tgmodemname" ]; then
        echo "custmodem=$custmodem not $wgmodemname or $tgmodemname, err!!!"
        exit;
    fi

    #get OP config: OP01_SPEC0200_SEGC(for CMCC), OPO2_SPEC0200_SEGA(for CU), NONE(for customize)
    custop=`cat joytech/$custproj/mediatek-config-$mtkproj-ProjectConfig.mk | grep "^\s*OPTR_SPEC_SEG_DEF" | sed 's/.*\s*=\s*//g'`;
    custop=`echo $custop`;
    #echo "custop=$custop";
    if [ "$custop" != "$op01" -a "$custop" != "$op02" -a "$custop" != "$opnone" ]; then
        echo "custop=$custop not $op01 or $op02 or $opnone, err!!!"
        exit;
    fi

    #MICRO_JT_PLATFORM_MT6572=`cat joytech/$custproj/mediatek-config-$mtkproj-ProjectConfig.mk | grep "^\s*JT_PLATFORM_MT6572" | sed 's/.*\s*=\s*//g'`;
    #if [ "$MICRO_JT_PLATFORM_MT6572" == "yes" ]; then
      #echo $MICRO_JT_PLATFORM_MT6572
    #fi
}

function fun_curtime () #show datetime
{  
    date '+%Y-%m-%d %H:%M:%S'  
} 

function fun_custproj_cp2alps () #copy custproj files to alps, use: fun DEFAULT|NODEFAULT 
{

    # for remove NativeMR proj eclipse create dir, avoid build err
    rm -rf development/samples/NativeMR/assets/
    rm -rf development/samples/NativeMR/gen/
    rm -rf development/samples/NativeMR/bin/

    if [ "$1" == "DEFAULT" ]; then
        cp joytech/zzdefault/mediatek-config-joytech72_wet_jb3-ProjectConfig.mk  mediatek/config/joytech72_wet_jb3/ProjectConfig.mk
        cp joytech/zzdefault/mediatek-config-joytech72_wet_lca-ProjectConfig.mk  mediatek/config/joytech72_wet_lca/ProjectConfig.mk
        cp joytech/zzdefault/mediatek-custom-joytech72_wet_jb3-kernel-dct-dct-codegen.dws  mediatek/custom/joytech72_wet_jb3/kernel/dct/dct/codegen.dws
        cp joytech/zzdefault/mediatek-custom-joytech72_wet_lca-kernel-dct-dct-codegen.dws  mediatek/custom/joytech72_wet_lca/kernel/dct/dct/codegen.dws

        rm -rf mediatek/custom/common/uboot/logo/custLogo
        rm -rf mediatek/custom/common/lk/logo/custLogo
        cp -rf joytech/zzdefault/mediatek-custom-common-uboot-logo-custLogo mediatek/custom/common/uboot/logo/custLogo
        cp -rf joytech/zzdefault/mediatek-custom-common-uboot-logo-custLogo mediatek/custom/common/lk/logo/custLogo

        rm -rf vendor/custApp/
        rm -rf frameworks/base/data/custRes/    
                
        rm -rf mediatek/operator/OP02/packages/apps/* 
        rm -rf mediatek/operator/OP02/prebuilt/*
        tar xzf joytech/zzdefault/OP02/mediatek-operator-OP02-packages-apps.tar.gz -C mediatek/operator/OP02/packages/apps/
        tar xzf joytech/zzdefault/OP02/mediatek-operator-OP02-prebuilt.tar.gz -C mediatek/operator/OP02/prebuilt/
        cp joytech/zzdefault/OP02/mediatek-operator-OP02-SPEC0200-SEGA-optr_apk_config.mk  mediatek/operator/OP02/SPEC0200/SEGA/optr_apk_config.mk
        echo "set origin default files to alps Ok!";
    else
        # ProjectConfig.mk
        if [ -f joytech/$custproj/mediatek-config-joytech72_wet_jb3-ProjectConfig.mk ]; then
            cp joytech/$custproj/mediatek-config-joytech72_wet_jb3-ProjectConfig.mk  mediatek/config/joytech72_wet_jb3/ProjectConfig.mk
        else
            cp joytech/zzdefault/mediatek-config-joytech72_wet_jb3-ProjectConfig.mk  mediatek/config/joytech72_wet_jb3/ProjectConfig.mk
        fi
        if [ -f joytech/$custproj/mediatek-config-joytech72_wet_lca-ProjectConfig.mk ]; then
            cp joytech/$custproj/mediatek-config-joytech72_wet_lca-ProjectConfig.mk  mediatek/config/joytech72_wet_lca/ProjectConfig.mk
        else            
            cp joytech/zzdefault/mediatek-config-joytech72_wet_lca-ProjectConfig.mk  mediatek/config/joytech72_wet_lca/ProjectConfig.mk
        fi
        
        # codegen.dws
        if [ -f joytech/$custproj/mediatek-custom-joytech72_wet_jb3-kernel-dct-dct-codegen.dws ]; then
            cp joytech/$custproj/mediatek-custom-joytech72_wet_jb3-kernel-dct-dct-codegen.dws  mediatek/custom/joytech72_wet_jb3/kernel/dct/dct/codegen.dws
        else            
            cp joytech/zzdefault/mediatek-custom-joytech72_wet_jb3-kernel-dct-dct-codegen.dws  mediatek/custom/joytech72_wet_jb3/kernel/dct/dct/codegen.dws
        fi
        if [ -f joytech/$custproj/mediatek-custom-joytech72_wet_lca-kernel-dct-dct-codegen.dws ]; then
            cp joytech/$custproj/mediatek-custom-joytech72_wet_lca-kernel-dct-dct-codegen.dws  mediatek/custom/joytech72_wet_lca/kernel/dct/dct/codegen.dws
        else            
            cp joytech/zzdefault/mediatek-custom-joytech72_wet_lca-kernel-dct-dct-codegen.dws  mediatek/custom/joytech72_wet_lca/kernel/dct/dct/codegen.dws
        fi
        
        # uboot/lk logo dir
        rm -rf mediatek/custom/common/uboot/logo/custLogo
        rm -rf mediatek/custom/common/lk/logo/custLogo
        if [ -d joytech/$custproj/mediatek-custom-common-uboot-logo-custLogo ]; then
            cp -rf joytech/$custproj/mediatek-custom-common-uboot-logo-custLogo mediatek/custom/common/uboot/logo/custLogo
            cp -rf joytech/$custproj/mediatek-custom-common-uboot-logo-custLogo mediatek/custom/common/lk/logo/custLogo
        else
            cp -rf joytech/zzdefault/mediatek-custom-common-uboot-logo-custLogo mediatek/custom/common/uboot/logo/custLogo
            cp -rf joytech/zzdefault/mediatek-custom-common-uboot-logo-custLogo mediatek/custom/common/lk/logo/custLogo
        fi
	
        rm -rf vendor/custApp/
        rm -rf frameworks/base/data/custRes/
        if [ "$custop" == "$op01" ]; then
            echo "no found OP01, Err!!!";
        elif [ "$custop" == "$op02" ]; then
            # operator-OP02-packages-apps, default apps dir
            rm -rf mediatek/operator/OP02/packages/apps/* 
            if [ -f joytech/$custproj/OP02/mediatek-operator-OP02-packages-apps.tar.gz ]; then
                tar xzf joytech/$custproj/OP02/mediatek-operator-OP02-packages-apps.tar.gz -C mediatek/operator/OP02/packages/apps/
            else
                tar xzf joytech/zzdefault/OP02/mediatek-operator-OP02-packages-apps.tar.gz -C mediatek/operator/OP02/packages/apps/
            fi
            # operator-OP02-prebuilt, include bootanimation/bootaudio dir
            rm -rf mediatek/operator/OP02/prebuilt/*
            if [ -f joytech/$custproj/OP02/mediatek-operator-OP02-prebuilt.tar.gz ]; then
                tar xzf joytech/$custproj/OP02/mediatek-operator-OP02-prebuilt.tar.gz -C mediatek/operator/OP02/prebuilt/
            else 
                tar xzf joytech/zzdefault/OP02/mediatek-operator-OP02-prebuilt.tar.gz -C mediatek/operator/OP02/prebuilt/
            fi
            # optr_apk_config.mk, apps config file
            if [ -f joytech/$custproj/OP02/mediatek-operator-OP02-SPEC0200-SEGA-optr_apk_config.mk ]; then
                cp joytech/$custproj/OP02/mediatek-operator-OP02-SPEC0200-SEGA-optr_apk_config.mk  mediatek/operator/OP02/SPEC0200/SEGA/optr_apk_config.mk
            else            
                cp joytech/zzdefault/OP02/mediatek-operator-OP02-SPEC0200-SEGA-optr_apk_config.mk  mediatek/operator/OP02/SPEC0200/SEGA/optr_apk_config.mk
            fi
        elif [ "$custop" == "$opnone" ]; then
            # custApp
            if [ -f joytech/$custproj/OPcust/vendor-custApp.tar.gz ]; then
                mkdir vendor/custApp/
                tar xzf joytech/$custproj/OPcust/vendor-custApp.tar.gz -C vendor/custApp/
            fi
            # custRes
            if [ -f joytech/$custproj/OPcust/frameworks-base-data-custRes.tar.gz ]; then
                mkdir frameworks/base/data/custRes/
                tar xzf joytech/$custproj/OPcust/frameworks-base-data-custRes.tar.gz -C frameworks/base/data/custRes/
            fi            
        fi
        
        echo "copy joytech/$custproj files to alps Ok!";
    fi
}

function fun_change () #chg cust default project, use: fun file srcStr destStr
{  
ed - $1<<!
1,g/$2/s//$3/g
w
q
!
#echo "fun_change: replace $2 to $3 in file($1)!";
} 

function fun_custproj_chg_default () #user chose project to set as default project
{ 
    arrayStr="";
    Folder="`pwd`/joytech";
    for file in ${Folder}/*; do  
        tempfile=`basename $file`;
        if [ "$tempfile" != "zzdefault" -a "$tempfile" != "custProjCfg" ]; then
            if [ "$tempfile" == "$custproj" ]; then
                arrayStr="$arrayStr $tempfile[DFT]";
            else
                arrayStr="$arrayStr $tempfile";
            fi    
        fi 
    done
    arrayStr="$arrayStr QUIT";
        
    PS3="Please select a choice: ";
    select entry in $arrayStr; do 
        if [ "$entry" == "" ]; then
            echo -e "Invalid input!"
            continue
        elif [ $entry == "QUIT" ]; then
            exit;
            break
        else
            #echo -e "you input is: $REPLY";
            if [ "$entry" == "$custproj[DFT]" ]; then
                entry=$custproj;
            fi
            fun_change $custProjCfg $custproj $entry
            custproj=$entry;
            fun_update_params_by_custproj
            echo "set default custproj=$custproj Ok!";
            break        
        fi        
    done
}

function fun_cp_bin () #copy bin to ~/version_save/projName/
{
    if [ ! -d ~/work ]; then
        mkdir ~/work
    fi  
    if [ ! -d ~/work/version_save ]; then
        mkdir ~/work/version_save
    fi  
    if [ -z "$custproj" ]; then
        destDir=${mtkproj};
    elif [ "$mtkproj" == "joytech72_wet_jb3" ]; then
        destDir=${custproj}-jb3;
    else
        destDir=${custproj}-lca;
    fi
    if [ "$engMode" -eq "1" ]; then
        destDir=${destDir}-eng
    else
        destDir=${destDir}-usr
    fi
    
    #copy bin
    if [ -d out/target/product/${mtkproj} ]; then 
        #del bin
        rm -rf ~/work/version_save/${destDir}
        mkdir  ~/work/version_save/${destDir}
        #copy bin
        chmod a+r out/target/product/${mtkproj}/*
        cp -fL out/target/product/${mtkproj}/*   ~/work/version_save/${destDir}
        #copy rf file: BPLGUInfoCustomAppSrcP_MT6572_S00_MOLY_WR8_W1315_MD_WG_MP_V4_P2_1_wg_n, APDB_MT6572_S01_MAIN2.1_W10.24
        cp mediatek/custom/common/modem/$custmodem/BPL*  ~/work/version_save/${destDir}
        cp mediatek/cgen/APDB* ~/work/version_save/${destDir}
        rm ~/work/version_save/${destDir}/APDB*_ENUM
        echo "copy bin/bpl/apdb files to ~/work/version_save/${destDir} success! $(fun_curtime)"
    else
        echo "copy bin/bpl/apdb files to ~/work/version_save/${destDir} Err!!!"
    fi
}

function fun_show_help () #show help
{
    echo "./m                   user build project guide one-by-one"
    echo "./m cp                copy bin/bpl/apdb to ~/version_save/custproj-jb3/lca-eng/usr"
    echo "./m set               set custom default proj, copy joytech/custproj files to alps"
    echo "./m setdft            set alps to default origin project, copy joytech/zzdefault files to alps"
    echo "./m listp             show mtk proj and custom proj"
    echo "./m [usr-]n           ./mk [-o=TARGET_BUILD_VARIANT=user] prj new"
    echo "./m [usr-]r           ./mk [-o=TARGET_BUILD_VARIANT=user] prj remake"
    echo "./m [usr-]c           ./mk [-o=TARGET_BUILD_VARIANT=user] prj clean"
    echo "./m [usr-]si          ./mk [-o=TARGET_BUILD_VARIANT=user] prj systemimage"
    echo "./m [usr-]kl          ./mk [-o=TARGET_BUILD_VARIANT=user] prj n kernel && ./mk [-o=TARGET_BUILD_VARIANT=user] prj bootimage"
    echo "./m [usr-]kl-r        ./mk [-o=TARGET_BUILD_VARIANT=user] prj r kernel && ./mk [-o=TARGET_BUILD_VARIANT=user] prj bootimage"
    echo "./m [usr-]drv         ./mk [-o=TARGET_BUILD_VARIANT=user] prj n preloader lk kernel && ./mk [-o=TARGET_BUILD_VARIANT=user] prj bootimage uboot"
    echo "./m [usr-]drv-r       ./mk [-o=TARGET_BUILD_VARIANT=user] prj r preloader lk kernel && ./mk [-o=TARGET_BUILD_VARIANT=user] prj bootimage uboot"
    echo "./m [usr-]mm md1 ..   ./mk [-o=TARGET_BUILD_VARIANT=user] prj mm module1 module2 .."
    echo "./m [op] prj act [md] ./mk [options] project actions [modules], other use: ./m = ./mk"
    echo "./m wg-modem          build wg modem and copy to project"
    echo "./m tg-modem          build tg modem and copy to project"
    echo "./m .                 open cur dir"
    echo "./m pc                open ProjectConfig.mk file"
    echo "./m pt                open partition_table_MT6572.xml dir"
    echo "./m md                open MemoryDeviceList_MT6572.xls dir"
    echo "./m bp                open build.prop file"
    echo "./m out               open out dir"
    echo "./m app               open out/target/product/proj/system/app/*.apk dir"
    echo "./m dws               open dws dir"
    echo "./m bpl               open RF BPL... dir"
    echo "./m apdb              open RF APDB... dir"
    echo "./m rf                open RF modem l1d_custom_rf.h/ul1d_custom_rf.h/tl1d_custom_rf.h dir"
}





###############start: check env, get custproj name###################################
if [ ! -d joytech -o ! -f $custProjCfg ]; then
    echo "not exist ./joytech dir or not find ./${custProjCfg} file, err!!!"
    exit;
fi
custproj=`cat $custProjCfg | grep "^\s*defaultproject" | sed 's/.*\s*=\s*//g'`;
custproj=`echo $custproj`;
#echo "custproj=$custproj";
find_custproj="0";
Folder="`pwd`/joytech";
for file in ${Folder}/*; do  
    tempfile=`basename $file`  
    if [ "$tempfile" != "zzdefault" -a "$tempfile" != "custProjCfg" ]; then
        if [ $tempfile == $custproj ]; then
            find_custproj="1";
        fi
    fi 
done
if [ "$find_custproj" -eq "0" ]; then
    echo "custproj=$custproj not found, err!!!"
    exit;
fi
fun_update_params_by_custproj    #get custproj params



#######################no param copy bin to ~/work/version_save/projname############
engMode="1"; #is user or eng, for fun_cp_bin() call use
rtnRslt="1"; #return value,decide call fun_cp_bin() or not
if [ -z "$1" ]; then #no param
    fun_custproj_chg_default
    ./m n
    bash
    exit;
fi


######################copy custproj files to alps###################################
if [ -n "$custproj" ]; then
  if [ "$1" != "help" -a "$1" != "cp" -a "$1" != "set" -a "$1" != "setdft" -a "$1" != "listp" -a "$1" != "." -a "$1" != "out" -a "$1" != "app" -a "$1" != "dws" -a "$1" != "bpl" -a "$1" != "apdb" -a "$1" != "rf" -a "$1" != "pt" -a "$1" != "md" -a "$1" != "pc" -a "$1" != "bp" -a "$1" != "wg-modem" -a "$1" != "tg-modem" ]; then 
    fun_custproj_cp2alps NODEFAULT
  fi
fi



########################1param,"n":new; "r":remake; "c":clean#######################
if [ -z "$2" ]; then #1 param
case "$1" in 
"help") #show help
fun_show_help
exit;
;;

"cp") #copy bin/bpl/apdb to ~/version_save/custproj-jb3/lca-eng/usr
fun_cp_bin
exit;
;;

"set") #set proj and set custproj
fun_custproj_chg_default
fun_custproj_cp2alps NODEFAULT
exit;
;;

"setdft") #set alps to default files
fun_custproj_cp2alps DEFAULT
exit;
;;

"listp")
echo "mtkproj in mediatek dir:"
Folder="`pwd`/mediatek/config";
for file in ${Folder}/*; do  
    tempfile=`basename $file`  
    if [ "$tempfile" != "common" -a "$tempfile" != "mt6572" -a "$tempfile" != "out" ]; then
        if [ "$tempfile" == "$mtkproj" ]; then
            echo "    $tempfile <-- DEFAULT";
        else
            echo "    $tempfile";
        fi
    fi 
done
echo "custproj in joytech dir:"
Folder="`pwd`/joytech";
for file in ${Folder}/*; do  
    tempfile=`basename $file`  
    if [ "$tempfile" != "zzdefault" -a "$tempfile" != "custProjCfg" ]; then
        if [ "$tempfile" == "$custproj" ]; then
            echo "    $tempfile <-- DEFAULT";
        else
            echo "    $tempfile";
        fi
    fi 
done
exit;
;;

".") #open cur dir
xdg-open .
exit;
;;

"out") #open out dir
xdg-open out/target/product
exit;
;;

"app") #open system/app/*.apk dir
xdg-open out/target/product/$mtkproj/system/app
exit;
;;

"dws") #open dws dir
xdg-open mediatek/custom/$mtkproj/kernel/dct/dct
exit;
;;

"pt") #open partition_table_MT6572.xml dir
xdg-open mediatek/build/tools/ptgen/MT6572/
exit;
;;

"md") #open MemoryDeviceList_MT6572.xls dir
xdg-open mediatek/build/tools/emigen/MT6572/
exit;
;;

"bpl") #open RF BPL... dir
xdg-open mediatek/custom/common/modem/
exit;
;;

"apdb") #open RF APDB... dir
xdg-open mediatek/cgen/
exit;
;;

"rf") #open RF modem .h dir
if [ $custmodem == $wgmodemname ]; then
xdg-open MOLY_GPRS_HSPA/custom/modem/
else
xdg-open MOLY_TDD128HSPA/custom/modem/
fi
exit;
;;

"pc") #open ProjectConfig.mk file
xdg-open joytech/$custproj/mediatek-config-$mtkproj-ProjectConfig.mk
exit;
;;

"bp") #open build.prop file
xdg-open  out/target/product/$mtkproj/system/build.prop
exit;
;;

"wg-modem") #build modem then cp modem 5 files to alps/mediatek/custom/modem/
rm -rf ./mediatek/custom/common/modem/$wgmodemname
mkdir ./mediatek/custom/common/modem/$wgmodemname
cd MOLY_GPRS_HSPA
rm -rf ./build
chmod +x ./tools/init/strcmpex_linux.exe
./make.sh "${wgmodemname}.mak" new
chmod +x ../mediatek/build/tools/modemRenameCopy.pl
../mediatek/build/tools/modemRenameCopy.pl ./ $wgmodemname
cp temp_modem/* ../mediatek/custom/common/modem/$wgmodemname/
cd ..
echo "build $wgmodemname modem and copy to project success! use \"./m [usr-]kl\" to take effect! $(fun_curtime)"
exit;
;;

"tg-modem") #build modem then cp modem 5 files to alps/mediatek/custom/modem/
rm -rf ./mediatek/custom/common/modem/$tgmodemname
mkdir ./mediatek/custom/common/modem/$tgmodemname
cd MOLY_TDD128HSPA
rm -rf ./build
chmod +x ./tools/init/strcmpex_linux.exe
./make.sh "${tgmodemname}.mak" new
chmod +x ../mediatek/build/tools/modemRenameCopy.pl
../mediatek/build/tools/modemRenameCopy.pl ./ $tgmodemname
echo "copy modem files to ./mediatek/custom/common/modem/"
cp temp_modem/* ../mediatek/custom/common/modem/$tgmodemname/
cd ..
echo "build $tgmodemname modem and copy to project success! use \"./m [usr-]kl\" to take effect! $(fun_curtime)"
exit;
;;

"n") 
echo "./mk $mtkproj new"
./mk $mtkproj n
rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    fun_cp_bin
fi
exit;
;;

"usr-n") 
engMode="0";
echo "./mk -o=TARGET_BUILD_VARIANT=user $mtkproj new"
./mk -o=TARGET_BUILD_VARIANT=user $mtkproj n
rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    fun_cp_bin
fi
exit;
;;

"r") 
echo "./mk $mtkproj remake"
./mk $mtkproj r
rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    fun_cp_bin
fi
exit;
;;

"usr-r") 
engMode="0";
echo "./mk -o=TARGET_BUILD_VARIANT=user $mtkproj r"
./mk -o=TARGET_BUILD_VARIANT=user $mtkproj r
rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    fun_cp_bin
fi
exit;
;;

"c") 
echo "./mk $mtkproj clean"
./mk $mtkproj c
exit;
;;

"usr-c") 
engMode="0";
echo "./mk -o=TARGET_BUILD_VARIANT=user $mtkproj clean"
./mk -o=TARGET_BUILD_VARIANT=user $mtkproj c
exit;
;;

"si") 
echo "./mk $mtkproj systemimage"
./mk systemimage
rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    fun_cp_bin
fi
exit;
;;

"usr-si") 
    engMode="0";
    echo "./mk -o=TARGET_BUILD_VARIANT=user $mtkproj systemimage"
    ./mk -o=TARGET_BUILD_VARIANT=user $mtkproj systemimage
    rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    fun_cp_bin
fi
exit;
;;

"kl") 
echo "./mk $mtkproj new kernel && copy bootimage"
./mk $mtkproj new kernel
rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    echo "mkbootimg --kernel kernel/out/kernel_$mtkproj.bin --ramdisk ./out/target/product/$mtkproj/ramdisk.img -o ./out/target/product/$mtkproj/boot.img"
    ./out/host/linux-x86/bin/mkbootimg --kernel kernel/out/kernel_$mtkproj.bin --ramdisk ./out/target/product/$mtkproj/ramdisk.img -o ./out/target/product/$mtkproj/boot.img
    fun_cp_bin
else
    echo "./mk $mtkproj new kernel && copy bootimage ------> err!!!"
fi
exit;
;;

"usr-kl") 
engMode="0";
echo "./mk -o=TARGET_BUILD_VARIANT=user $mtkproj new kernel && copy bootimage"
./mk -o=TARGET_BUILD_VARIANT=user $mtkproj new kernel
rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    echo "mkbootimg --kernel kernel/out/kernel_$mtkproj.bin --ramdisk ./out/target/product/$mtkproj/ramdisk.img -o ./out/target/product/$mtkproj/boot.img"
    ./out/host/linux-x86/bin/mkbootimg --kernel kernel/out/kernel_$mtkproj.bin --ramdisk ./out/target/product/$mtkproj/ramdisk.img -o ./out/target/product/$mtkproj/boot.img
    fun_cp_bin
else
    echo "./mk -o=TARGET_BUILD_VARIANT=user $mtkproj new kernel && copy bootimage ------> err!!!"
fi
exit;
;;

"kl-r") 
echo "./mk $mtkproj r kernel && copy bootimage"
./mk $mtkproj r kernel
rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    echo "mkbootimg --kernel kernel/out/kernel_$mtkproj.bin --ramdisk ./out/target/product/$mtkproj/ramdisk.img -o ./out/target/product/$mtkproj/boot.img"
    ./out/host/linux-x86/bin/mkbootimg --kernel kernel/out/kernel_$mtkproj.bin --ramdisk ./out/target/product/$mtkproj/ramdisk.img -o ./out/target/product/$mtkproj/boot.img
    fun_cp_bin
else
    echo "./mk $mtkproj r kernel && copy bootimage ------> err!!!"
fi
exit;
;;

"usr-kl-r") 
engMode="0";
echo "./mk -o=TARGET_BUILD_VARIANT=user $mtkproj r kernel && copy bootimage"
./mk -o=TARGET_BUILD_VARIANT=user $mtkproj r kernel
rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    echo "mkbootimg --kernel kernel/out/kernel_$mtkproj.bin --ramdisk ./out/target/product/$mtkproj/ramdisk.img -o ./out/target/product/$mtkproj/boot.img"
    ./out/host/linux-x86/bin/mkbootimg --kernel kernel/out/kernel_$mtkproj.bin --ramdisk ./out/target/product/$mtkproj/ramdisk.img -o ./out/target/product/$mtkproj/boot.img
    fun_cp_bin
else
    echo "./mk -o=TARGET_BUILD_VARIANT=user $mtkproj r kernel && copy bootimage ------> err!!!"
fi
exit;
;;

"drv") 
echo "./mk $mtkproj new preloader lk kernel && ./mk $mtkproj bootimage uboot"
./mk $mtkproj new preloader lk kernel
rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    echo "./mk $mtkproj new preloader lk kernel ok! now ./mk $mtkproj bootimage uboot"
    ./mk $mtkproj bootimage uboot
    rtnRslt=$?;
fi
if [ "$rtnRslt" -eq "0" ]; then
    fun_cp_bin
else
    echo "./mk $mtkproj new preloader lk kernel && ./mk $mtkproj bootimage uboot ------> err!!!"
fi
exit;
;;

"usr-drv") 
engMode="0";
echo "./mk -o=..user $mtkproj new preloader lk kernel && ./mk -o=..user $mtkproj bootimage uboot"
./mk -o=TARGET_BUILD_VARIANT=user $mtkproj new preloader lk kernel
rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    echo "./mk -o=..user $mtkproj new preloader lk kernel ok! now ./mk -o=..user $mtkproj bootimage uboot"
    ./mk -o=TARGET_BUILD_VARIANT=user $mtkproj bootimage uboot
    rtnRslt=$?;
fi
if [ "$rtnRslt" -eq "0" ]; then
    fun_cp_bin
else
    echo "./mk -o=..user $mtkproj new preloader lk kernel && ./mk -o=..user $mtkproj bootimage uboot ------> err!!!"
fi
exit;
;;

"drv-r") 
echo "./mk $mtkproj r preloader lk kernel && ./mk $mtkproj bootimage uboot"
./mk $mtkproj r preloader lk kernel
rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    echo "./mk $mtkproj r preloader lk kernel ok! now ./mk $mtkproj bootimage uboot"
    ./mk $mtkproj bootimage uboot
    rtnRslt=$?;
fi
if [ "$rtnRslt" -eq "0" ]; then
    fun_cp_bin
else
    echo "./mk $mtkproj r preloader lk kernel && ./mk $mtkproj bootimage uboot ------> err!!!"
fi
exit;
;;

"usr-drv-r") 
engMode="0";
echo "./mk -o=..user $mtkproj r preloader lk kernel && ./mk -o=..user $mtkproj bootimage uboot"
./mk -o=TARGET_BUILD_VARIANT=user $mtkproj r preloader lk kernel
rtnRslt=$?;
if [ "$rtnRslt" -eq "0" ]; then
    echo "./mk -o=..user $mtkproj r preloader lk kernel ok! now ./mk -o=..user $mtkproj bootimage uboot"
    ./mk -o=TARGET_BUILD_VARIANT=user $mtkproj bootimage uboot
    rtnRslt=$?;
fi
if [ "$rtnRslt" -eq "0" ]; then
    fun_cp_bin
else
    echo "./mk -o=..user $mtkproj r preloader lk kernel && ./mk -o=..user $mtkproj bootimage uboot ------> err!!!"
fi
exit;
;;

*) 
;;
esac #end case

fi #end if





################other case,call ./mk#######################################
if [ "$1" == "usr-mm" ]; then
    engMode="0";
    echo "./mk -o=TARGET_BUILD_VARIANT=user mm $2 $3 $4 $5 $6 $7 $8 $9"
    ./mk -o=TARGET_BUILD_VARIANT=user mm $2 $3 $4 $5 $6 $7 $8 $9
    rtnRslt=$?;
else
    if [ "$1" == "-o=TARGET_BUILD_VARIANT=user" ]; then
        engMode="0";
    fi
    echo "./mk $1 $2 $3 $4 $5 $6 $7 $8 $9"
    ./mk $1 $2 $3 $4 $5 $6 $7 $8 $9
    rtnRslt=$?;
fi

if [ "$rtnRslt" -eq "0" ]; then
isNewRemake="0";
case "$1" in  #param1 is n,new,r,remake,bootimage,uboot,systemimage...?
"n")
isNewRemake="1";
;;
"new")
isNewRemake="1";
;;
"r")
isNewRemake="1";
;;
"remake")
isNewRemake="1";
;;
"bootimage")
isNewRemake="1";
;;
"uboot")
isNewRemake="1";
;;
"systemimage")
isNewRemake="1";
;;
esac #end case

case "$2" in  #param2 is n,new,r,remake,bootimage,uboot,systemimage...?
"n")
isNewRemake="1";
;;
"new")
isNewRemake="1";
;;
"r")
isNewRemake="1";
;;
"remake")
isNewRemake="1";
;;
"bootimage")
isNewRemake="1";
;;
"uboot")
isNewRemake="1";
;;
"systemimage")
isNewRemake="1";
;;
esac #end case

case "$3" in #param3 is n,new,r,remake,bootimage,uboot,systemimage...?
"n")
isNewRemake="1";
;;
"new")
isNewRemake="1";
;;
"r")
isNewRemake="1";
;;
"remake")
isNewRemake="1";
;;
"bootimage")
isNewRemake="1";
;;
"uboot")
isNewRemake="1";
;;
"systemimage")
isNewRemake="1";
;;
esac #end case

if [ "$isNewRemake" -eq "1" ]; then
    fun_cp_bin
fi
fi
######################end####################################################


